\documentclass{article}

\begin{document}
\SweaveOpts{concordance=TRUE}

<<>>=

## This file replicates all figures and tables in the main text and the appendix of:
## Sharan Grewal, "The Islamist Advantage: The Religious Infrastructure of Electoral Victory,"
## British Journal of Political Science.
## Contact sgrewal@american.edu for any questions.


## Load packages
library(stargazer)
library(effects)
library(sjPlot)
library(sjlabelled)
library(sjmisc)
library(ggplot2)
library(scales)
library(mediation)


## Load data
parties <- read.csv("parties.csv")
AB <- read.csv("AB.csv")
survey2020 <- read.csv("survey2020.csv")
data <- read.csv("data.csv")

@





<<>>=

## Figure 1: The Resilience of Ennahda, 2011-2019


## Figure 1A: Vote Share of Coalition Partners, 2011 to 2014
plot(c(1,2), parties$Ennahda[c(1:2)], las=1, xaxt="n", type="b", col="green4", lwd=3, xlab="", ylab="Vote Share", ylim=c(-3,50), xlim=c(0.75,2.25), main="Vote Share of Coalition Partners, 2011-2014")
axis(side=1, at=c(1,2), labels=c("2011","2014"))
lines(c(1,2), parties$CPR[c(1:2)], type="b", col="blue", lwd=1, lty=2)
lines(c(1,2), parties$Ettakatol[c(1:2)], type="b", col="red3", lwd=1, lty=2)

text(c(1,2), parties$Ennahda[c(1:2)], label=round(parties$Ennahda[c(1:2)],0), pos=3)
text(c(1,2), parties$CPR[c(1:2)], label=round(parties$CPR[c(1:2)],0), pos=3)
text(c(1,2), parties$Ettakatol[c(1:2)], label=round(parties$Ettakatol[c(1:2)],0), pos=1)

legend(1.8,51,c("Ennahda","CPR","Ettakatol"), lwd=c(3,1,1), lty=c(1,2,2), col=c("green4","blue","red3"), y.intersp=0.75, cex=0.75)


## Figure 1B: Vote Share of Coalition Partners, 2014 to 2019

plot(c(1,2), parties$Ennahda[c(2:3)], las=1, xaxt="n", type="b", col="green4", lwd=3, xlab="", ylab="Vote Share", ylim=c(-3,50), xlim=c(0.75,2.25), main="Vote Share of Coalition Partners, 2014-2019")
axis(side=1, at=c(1,2), labels=c("2014","2019"))
lines(c(1,2), parties$Nidaa[c(2:3)], type="b", col="red3", lwd=1, lty=2)
lines(c(1,2), parties$UPL[c(2:3)], type="b", col="blue", lwd=1, lty=2)
lines(c(1,2), parties$Afek[c(2:3)], type="b", col="black", lwd=1, lty=2)

text(c(1,2), parties$Ennahda[c(2:3)], label=round(parties$Ennahda[c(2:3)],0),pos=3,col="green4")
text(c(1,2), parties$Nidaa[c(2:3)], label=round(parties$Nidaa[c(2:3)],0),pos=c(3,3),col="red3")
text(c(1,2), parties$UPL[c(2:3)], label=round(parties$UPL[c(2:3)],0), pos=c(3,1), col="blue")
text(c(1,2), parties$Afek[c(2:3)], label=round(parties$Afek[c(2:3)],0), pos=c(1,4), col="black")

legend(1.8,51,c("Ennahda","Nidaa","UPL","Afek"), lwd=c(3,1,1,1), lty=c(1,2,2,2), col=c("green4","red3","blue","black"), y.intersp=0.75, cex=0.75)


@



<<>>=

####################
## Arab Barometer ##
####################


## Figure 2: Mosque attendees are more likely to want Islamist candidates (Arab Barometer)
## Using attendance at religious lessons

data.summary <- cbind(tapply(AB$signal_mosque[AB$wave=="W2"]=="1. to a great extent", AB$mosque2[AB$wave=="W2"], mean, na.rm=T),
tapply(AB$signal_friday[AB$wave=="W2"]=="1. to a great extent", AB$mosque2[AB$wave=="W2"], mean, na.rm=T),
tapply(AB$cand_piety[AB$wave=="W2"]>4, AB$mosque2[AB$wave=="W2"], mean, na.rm=T),
tapply(AB$piety_most[AB$wave=="W2"], AB$mosque2[AB$wave=="W2"], mean, na.rm=T),
tapply(AB$relpub[AB$wave=="W2"]>2, AB$mosque2[AB$wave=="W2"], mean, na.rm=T),
tapply(AB$sharia_penal[AB$wave=="W2"]>2, AB$mosque2[AB$wave=="W2"], mean, na.rm=T))*100

par(mar = c(5, 6.2, 4, 2) + 0.1)

barplot <- barplot(data.summary, beside=T, main="Demand for Islamists by Mosque Attendance\n(Arab Barometer 2011, N=12,208)", names.arg=rev(c("Want penal laws\nconform w/ sharia","Want religious\npeople in gov", "Candidate's piety\nmost imp. factor","Candidate's piety\namong top 3","To signal piety,\nattend Jumu`ah","To signal piety,\nattend mosque")), cex.names=0.8, horiz=T, las=1, xlim=c(0,100), xlab="Percent", cex=0.9)
text(y=barplot, x=data.summary, round(data.summary,0), pos=4, cex=0.5)
legend(x=70, y=20, title="Do you attend mosque?", legend=c("Always","Often","Sometimes","Rarely"), cex=0.7, fill=rev(gray.colors(4)))



## Figure A.2: Mosque attendees are more likely to want Islamist candidates (Arab Barometer)
## Using Friday prayers

data.summary <- cbind(tapply(AB$signal_mosque[AB$wave=="W2"]=="1. to a great extent", AB$friday2[AB$wave=="W2"], mean, na.rm=T),
tapply(AB$signal_friday[AB$wave=="W2"]=="1. to a great extent", AB$friday2[AB$wave=="W2"], mean, na.rm=T),
tapply(AB$cand_piety[AB$wave=="W2"]>4, AB$friday2[AB$wave=="W2"], mean, na.rm=T),
tapply(AB$piety_most[AB$wave=="W2"], AB$friday2[AB$wave=="W2"], mean, na.rm=T),
tapply(AB$relpub[AB$wave=="W2"]>2, AB$friday2[AB$wave=="W2"], mean, na.rm=T),
tapply(AB$sharia_penal[AB$wave=="W2"]>2, AB$friday2[AB$wave=="W2"], mean, na.rm=T))*100

par(mar = c(5, 6.2, 4, 2) + 0.1)

barplot <- barplot(data.summary, beside=T, main="Demand for Islamists by Mosque Attendance\n(Arab Barometer 2011, N=12,117)", names.arg=rev(c("Want penal laws\nconform w/ sharia","Want religious\npeople in gov", "Candidate's piety\nmost imp. factor","Candidate's piety\namong top 3","To signal piety,\nattend Jumu`ah","To signal piety,\nattend mosque")), cex.names=0.8, horiz=T, las=1, xlim=c(0,100), xlab="Percent", cex=0.9)
text(y=barplot, x=data.summary, round(data.summary,0), pos=4, cex=0.5)
legend(x=70, y=20, title="Do you attend Jumu`ah?", legend=c("Always","Often","Sometimes","Rarely"), cex=0.7, fill=rev(gray.colors(4)))




## Figure A.3: Mosque attendance and Islamist appeal in Tunisia (Arab Barometer)

data.summary <- cbind(tapply(AB$signal_mosque[AB$wave=="W2" & AB$country=="Tunisia"]=="1. to a great extent", AB$mosque2[AB$wave=="W2" & AB$country=="Tunisia"], mean, na.rm=T),
tapply(AB$signal_friday[AB$wave=="W2" & AB$country=="Tunisia"]=="1. to a great extent", AB$mosque2[AB$wave=="W2" & AB$country=="Tunisia"], mean, na.rm=T),
tapply(AB$cand_piety[AB$wave=="W2" & AB$country=="Tunisia"]>4, AB$mosque2[AB$wave=="W2" & AB$country=="Tunisia"], mean, na.rm=T),
tapply(AB$piety_most[AB$wave=="W2" & AB$country=="Tunisia"], AB$mosque2[AB$wave=="W2" & AB$country=="Tunisia"], mean, na.rm=T),
tapply(AB$relpub[AB$wave=="W2" & AB$country=="Tunisia"]>2, AB$mosque2[AB$wave=="W2" & AB$country=="Tunisia"], mean, na.rm=T),
tapply(AB$sharia_penal[AB$wave=="W2" & AB$country=="Tunisia"]>2, AB$mosque2[AB$wave=="W2" & AB$country=="Tunisia"], mean, na.rm=T))*100

par(mar = c(5, 6.2, 4, 2) + 0.1)

barplot <- barplot(data.summary, beside=T, main="Demand for Islamists by Mosque Attendance\n(Arab Barometer 2011, Tunisia, N=1175)", names.arg=rev(c("Want penal laws\nconform w/ sharia","Want religious\npeople in gov", "Candidate's piety\nmost imp. factor","Candidate's piety\namong top 3","To signal piety,\nattend Jumu`ah","To signal piety,\nattend mosque")), cex.names=0.8, horiz=T, las=1, xlim=c(0,100), xlab="Percent", cex=0.9)
text(y=barplot, x=data.summary, round(data.summary,0), pos=4, cex=0.5)
legend(x=70, y=20, title="Do you attend mosque?", legend=c("Always","Often","Sometimes","Rarely"), cex=0.7, fill=rev(gray.colors(4)))



@



<<>>=

#############################
## Test 1: Jan 2020 Survey ##
#############################


## Descriptive statistics in text
mean(survey2020$votedparli) # 44%
mean(survey2020$voted_nahda[survey2020$votedparli==1]) # 19.9%


## Table A.2: Mosque Attendance, Voting for Ennahda, and Trust in Ennahda

one <- lm(voted_nahda~attend_mosque5, data=survey2020)

two <- lm(voted_nahda~attend_mosque5+female+age+unemp+edu+inc+econ_bad+corr_vhigh+democ+revo, data=survey2020)

three <- lm(voted_nahda~attend_mosque5+female+age+unemp+edu+inc+econ_bad+corr_vhigh+democ+revo+offerIsl+camp_mosque+imam+pray5times+rel+islamist, data=survey2020)

six <- lm(trust_nahda~attend_mosque5, data=survey2020)

four <- lm(trust_nahda~attend_mosque5+female+age+unemp+edu+inc+econ_bad+corr_vhigh+democ+revo+offerIsl+camp_mosque+imam+pray5times+rel+islamist, data=survey2020)

five <- lm(voted_nahda~attend_mosque5+female+age+unemp+edu+inc+econ_bad+corr_vhigh+democ+revo+offerIsl+camp_mosque+imam+pray5times+rel+islamist+trust_nahda, data=survey2020)

stargazer(one, three, six, four, five, covariate.labels=c("Mosque Attendance","Female","Age","Unemployment","Education","Income","Economy is Bad","Corruption is High","Support Democracy","Partic in Revolution","Ennahda offered goods","Campaigned in Mosques","Imam instructed","Freq. of Prayer","Hijab/Zabiba","Islamist","Trust in Ennahda"), dep.var.labels=c("Voted for Ennahda","Trust in Ennahda","Voted for Ennahda"))



## Figure 3: Mosque Attendance and Voting for Ennahda in the 2019 Elections

## Figure 3A: Bivariate
one <- lm(voted_nahda~attend_mosque, data=survey2020)

plot(effect("attend_mosque", one), main="Mosque Attendance and Voting for Ennahda\nin 2019 Parliamentary Elections", ylab="Vote Nahda", rug=F, xlab="Frequency of Mosque Attendance")

## Figure 3B: Multivariate

three <- lm(voted_nahda~attend_mosque5+female+age+unemp+edu+inc+econ_bad+corr_vhigh+democ+revo+offerIsl+camp_mosque+imam+pray5times+rel+islamist, data=survey2020)

plot_model(three, title="Voting for Ennahda in 2019 Parliamentary Elections", terms=c("attend_mosque5","female","age","unemp","edu","inc","econ_bad","corr_vhigh","democ","revo","offerIsl","camp_mosque","imam","pray5times","rel","islamist"), axis.labels=rev(c("Mosque attendance","Female","Age","Unemployed","Education","Income","Economy is bad","Corruption is high", "Support Democracy","Partic in Revolution","Ennahda offered goods","Campaigned at Mosque","Imam instructed","Freq of Prayer","Hijab/Zabiba","Islamist")), show.values=T, value.offset=.4, type = "std", dot.size=1, value.size=3.4) + ylim(-.3, .3) + set_theme(geom.outline.color = "antiquewhite4", base = theme_bw()) + geom_hline(yintercept=0, color="black", size=.2) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), panel.border = element_blank()) + theme(axis.line = element_line(color = 'black'))



## Figure 4: Mosque Attendence and Trust in Ennahda

## Figure 4A: Bivariate
three <- lm(trust_nahda~attend_mosque, data=survey2020)

plot(effect("attend_mosque", three), main="Mosque Attendance and Trust in Ennahda", ylab="Trust in Nahda (1-10)", rug=F, xlab="Frequency of Mosque Attendance")

## Figure 4B: Mediation Analysis

one <- lm(trust_nahda~attend_mosque5+female+age+unemp+edu+inc+econ_bad+corr_vhigh+democ+revo+offerIsl+camp_mosque+imam+pray5times+rel+islamist, data=survey2020[!is.na(survey2020$voted_nahda),])
two <- lm(voted_nahda~attend_mosque5+female+age+unemp+edu+inc+econ_bad+corr_vhigh+democ+revo+offerIsl+camp_mosque+imam+pray5times+rel+islamist+trust_nahda, data=survey2020)
med <- mediate(one, two, treat="attend_mosque5", mediator="trust_nahda", sims=1000, boot=T)

plot(med, main="Effect of Attending Mosque on Voting for Ennahda\nMediated by Trust in Ennahda", xlab="Voting for Ennahda", yaxt="n") 
text(x=c(med$d.avg, med$z.avg, med$tau.coef), y=c(3,2,1), labels=paste(round(c(med$d.avg, med$z.avg, med$tau.coef),3), c("***","","*"),sep=" "), pos=3)
axis(side=2, at=c(3,2,1), labels=c("Mediated\nEffect","Direct\nEffect","Total\nEffect"), las=2, cex.axis=0.9) # 70%


## Figure A.4 Causal Mediation Analysis
summary(med)



## Figure A.5: Mosque Attendence and Voting for Mourou in the 2019 Elections

## Figure A.5A: Bivariate
two <- lm(vote_mourou~attend_mosque, data=survey2020)

plot(effect("attend_mosque", two), main="Mosque Attendance and Voting for Abdelfattah Mourou\nin 2019 Presidential Elections (First Round)", ylab="Vote Mourou", rug=F, xlab="Frequency of Mosque Attendance")

## Figure A.5B: Mediation Analysis

one <- lm(trust_nahda~attend_mosque5+female+age+unemp+edu+inc+econ_bad+corr_vhigh+democ+revo+offerIsl+camp_mosque+imam+pray5times+rel+islamist, data=survey2020[!is.na(survey2020$vote_mourou),])
two <- lm(vote_mourou~attend_mosque5+female+age+unemp+edu+inc+econ_bad+corr_vhigh+democ+revo+offerIsl+camp_mosque+imam+pray5times+rel+islamist+trust_nahda, data=survey2020)
med2 <- mediate(one, two, treat="attend_mosque5", mediator="trust_nahda", sims=1000, boot=T)
summary(med2)

plot(med2, main="Effect of Attending Mosque on Voting for Abdelfattah Mourou\nMediated by Trust in Ennahda", xlab="Voting for Ennahda", yaxt="n") 
text(x=c(med2$d.avg, med2$z.avg, med2$tau.coef), y=c(3,2,1), labels=paste(round(c(med2$d.avg, med2$z.avg, med2$tau.coef),3), c("***","",""),sep=" "), pos=3)
axis(side=2, at=c(3,2,1), labels=c("Mediated\nEffect","Direct\nEffect","Total\nEffect"), las=2, cex.axis=0.9)




## Table A.3: Mosque Attendance and Voting for Secular Parties
one <- lm((parli=="Qalb")~attend_mosque5+female+age+unemp+edu+inc+econ_bad+corr_vhigh+democ+revo+offerIsl+camp_mosque+imam+pray5times+rel+islamist, data=survey2020)
two <- lm((parli=="Tayyar")~attend_mosque5+female+age+unemp+edu+inc+econ_bad+corr_vhigh+democ+revo+offerIsl+camp_mosque+imam+pray5times+rel+islamist, data=survey2020)
three <- lm((parli=="PDL")~attend_mosque5+female+age+unemp+edu+inc+econ_bad+corr_vhigh+democ+revo+offerIsl+camp_mosque+imam+pray5times+rel+islamist, data=survey2020)
four <- lm((parli=="Echaab")~attend_mosque5+female+age+unemp+edu+inc+econ_bad+corr_vhigh+democ+revo+offerIsl+camp_mosque+imam+pray5times+rel+islamist, data=survey2020)
five <- lm((parli=="Tahya")~attend_mosque5+female+age+unemp+edu+inc+econ_bad+corr_vhigh+democ+revo+offerIsl+camp_mosque+imam+pray5times+rel+islamist, data=survey2020)
six <- lm((parli=="Other")~attend_mosque5+female+age+unemp+edu+inc+econ_bad+corr_vhigh+democ+revo+offerIsl+camp_mosque+imam+pray5times+rel+islamist, data=survey2020)

library(stargazer)
stargazer(one, two, three, four, five, six, covariate.labels=c("Mosque Attendance","Female","Age","Unemployment","Education","Income","Economy is Bad","Corruption is High","Support Democracy","Partic in Revolution","Ennahda offered goods","Campaigned in Mosques","Imam instructed","Freq. of Prayer","Hijab/Zabiba","Islamist"), dep.var.labels=c("Qalb","Tayyar","PDL","Echaab","Tahya","Other"))




## Table A.4: Subsetting to Women

one <- lm(voted_nahda~attend_mosque5, data=survey2020[survey2020$female==1,])

two <- lm(voted_nahda~attend_mosque5+age+unemp+edu+inc+econ_bad+corr_vhigh+democ+revo, data=survey2020[survey2020$female==1,])

three <- lm(voted_nahda~attend_mosque5+age+unemp+edu+inc+econ_bad+corr_vhigh+democ+revo+offerIsl+camp_mosque+imam+pray5times+rel+islamist, data=survey2020[survey2020$female==1,])

six <- lm(trust_nahda~attend_mosque5, data=survey2020[survey2020$female==1,])

four <- lm(trust_nahda~attend_mosque5+age+unemp+edu+inc+econ_bad+corr_vhigh+democ+revo+offerIsl+camp_mosque+imam+pray5times+rel+islamist, data=survey2020[survey2020$female==1,])

five <- lm(voted_nahda~attend_mosque5+age+unemp+edu+inc+econ_bad+corr_vhigh+democ+revo+offerIsl+camp_mosque+imam+pray5times+rel+islamist+trust_nahda, data=survey2020[survey2020$female==1,])

library(stargazer)
stargazer(one, three, six, four, five, covariate.labels=c("Mosque Attendance","Age","Unemployment","Education","Income","Economy is Bad","Corruption is High","Support Democracy","Partic in Revolution","Ennahda offered goods","Campaigned in Mosques","Imam instructed","Freq. of Prayer","Hijab/Zabiba","Islamist","Trust in Ennahda"), dep.var.labels=c("Voted for Ennahda","Trust in Ennahda","Voted for Ennahda"))




## Table A.5: Comparing Ennahda and Ex-Ennahda voters

one <- lm(attend_mosque2~ennahda, data=survey2020)
two <- lm(attend_mosque2~ennahda+female+age+unemp+edu+inc+econ_bad+corr_high+democ+revo, data=survey2020)
three <- lm(attend_mosque2~ennahda+female+age+unemp+edu+inc+econ_bad+corr_high+democ+revo+offerIsl+camp_mosque+imam+pray5times+rel+islamist, data=survey2020)

stargazer(one, two, three, covariate.labels=c("Ex-Ennahda","Never Ennahda","Female","Age","Unemployment","Education","Income","Economy is Bad","Corruption is High","Support Democracy","Partic in Revolution","Ennahda offered goods","Campaigned in Mosques","Imam instructed","Freq. of Prayer","Hijab/Zabiba","Islamist"), dep.var.labels=c("Daily/Weekly Mosque Attendance"))


## Figure 5: Ennahda retained the more regular mosque-goers

data.summary <- data.frame(
  treatment=c("Ennahda", "Ex-Ennahda", "Never Ennahda"),
  mean=rbind(mean(survey2020$attend_mosque2[survey2020$ennahda=="Ennahda"], na.rm=T),
             mean(survey2020$attend_mosque2[survey2020$ennahda=="Ex-Ennahda"], na.rm=T), 
             mean(survey2020$attend_mosque2[survey2020$ennahda=="Never"], na.rm=T)),
  n=rbind(length(survey2020$attend_mosque2[survey2020$ennahda=="Ennahda"]),
          length(survey2020$attend_mosque2[survey2020$ennahda=="Ex-Ennahda"]), 
          length(survey2020$attend_mosque2[survey2020$ennahda=="Never"])),
  sd=rbind(sd(survey2020$attend_mosque2[survey2020$ennahda=="Ennahda"], na.rm=T),
           sd(survey2020$attend_mosque2[survey2020$ennahda=="Ex-Ennahda"], na.rm=T), 
           sd(survey2020$attend_mosque2[survey2020$ennahda=="Never"], na.rm=T)))

data.summary$treatment <- factor(data.summary$treatment, levels=c("Ennahda", "Ex-Ennahda", "Never Ennahda"))
data.summary$sem <- data.summary$sd/sqrt(data.summary$n)
data.summary$me <- qt(1-.05/2, df=data.summary$n)*data.summary$sem
data.summary$me90 <- qt(1-.1/2, df=data.summary$n)*data.summary$sem

ggplot(data.summary, 
       aes(x=treatment, y=mean)) +  
  geom_bar(position=position_dodge(width=0.5), stat="identity", 
           fill=c("darkblue","lightsteelblue","grey"), size=0.5) + 
  geom_errorbar(aes(ymin=mean-me, ymax=mean+me, width=0.1), size=0.2) +
  geom_errorbar(aes(ymin=mean-me90, ymax=mean+me90, width=0), size=1) +
  ggtitle("Percent Attending Mosque Daily or Weekly") +
  theme_bw() + 
  theme(panel.grid.major = element_blank()) + 
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_y_continuous(limits=c(20, 70),breaks=seq(20, 70, by=10),oob=rescale_none) +
  xlab("") +
  ylab("Percent") +
  geom_text(aes(x=2, y=70, label="* p=0.04"), size=4, col="blue") +
  geom_text(aes(x=3, y=70, label="*** p<0.001"), size=4, col="blue") +
  geom_text(data=data.summary, 
            aes(x=treatment, y=mean,
                label=formatC(round(mean,0),format='f',digits=0)),
            vjust=c(-5.2,-4.5, -2),size=5,position=position_dodge(0.9)) +
  theme(text = element_text(size=16))




@






<<>>=

##########################
## Test 2: Spatial Data ##
##########################


## Table A.6: Bivariate Relationship, Mosques and Ennahda Vote Share

one <- lm(nahda11~mosque_pc_11, data=data)
two <- lm(nahda14~mosque_pc_11, data=data)
three <- lm(nahda19~mosque_pc_11, data=data)
four <- lm(nahda19~mosque_pc_19, data=data)
five <- lm(nahda_change1119~mosque_pc_11, data=data)
six <- lm(nahda_change1119~mosque_pc_19, data=data)

library(stargazer)
stargazer(one, two, three, four, five, six, covariate.labels=c("Mosques per 10,000 (2011)", "Mosques per 10,000 (2019)"))



## Figure 8: Bivariate Relationship, Mosques and Ennahda Vote Share

## Figure 8A: Bivariate scatterplot, 2011
data$labels <- ifelse(data$nahda11>54 | data$mosque_pc_11>13, as.character(data$delegation), "")

data$pos <- ifelse(data$delegation=="Matmata", 2,
      ifelse(data$delegation=="Sidi Aïch", 3, 
      ifelse(data$delegation=="Zarzis" | data$delegation=="Tataouine Nord" | data$delegation=="Ben Guerdane", 1, 4)))

plot(y=data$nahda11, x=data$mosque_pc_11, main="2011 Nahda Vote Share by Mosque Density", ylab="Vote Share 2011", xlab="Mosques per 10,000 people (2011)")
abline(lm(nahda11~mosque_pc_11, data=data), col="blue")
text(nahda11~mosque_pc_11, labels=labels, cex=0.6, pos=pos, data=data)
text(x=19, y=42, label="p=0.015", col="blue")


## Figure 8B: Bivariate scatterplot, 2014
data$labels <- ifelse(data$nahda14>60 | data$mosque_pc_11>13, as.character(data$delegation), "")

data$pos <- ifelse(data$delegation=="Matmata" | data$delegation=="El Hamma" | data$delegation=="Tataouine Nord", 2,
      ifelse(data$delegation=="Sidi Aïch" | data$delegation=="Bab Souika" | data$delegation=="Tameghza", 3, 
      ifelse(data$delegation=="Ghomrassen" | data$delegation=="Remada" | data$delegation=="Medenine Nord" | data$delegation=="Medenine Sud", 1, 4)))

plot(y=data$nahda14, x=data$mosque_pc_11, main="2014 Nahda Vote Share by Mosque Density", ylab="Vote Share 2014", xlab="Mosques per 10,000 people (2011)")
abline(lm(nahda14~mosque_pc_11, data=data), col="blue")
text(nahda14~mosque_pc_11, labels=labels, cex=0.6, pos=pos, data=data)
text(x=20, y=40, label="p<0.001", col="blue")


## Figure 8C: Bivariate scatterplot, 2019
data$labels <- ifelse(data$nahda19>48 | data$mosque_pc_11>15, as.character(data$delegation), "")

data$pos <- ifelse(data$delegation=="Matmata", 2,
      ifelse(data$delegation=="Sidi Aïch" | data$delegation=="Smâr", 3, 
      ifelse(data$delegation=="Ben Guerdane" | data$delegation=="Bab Souika", 1, 4)))

plot(y=data$nahda19, x=data$mosque_pc_11, main="2019 Nahda Vote Share by Mosque Density", ylab="Vote Share 2019", xlab="Mosques per 10,000 people (2011)")
abline(lm(nahda19~mosque_pc_11, data=data), col="blue")
text(nahda19~mosque_pc_11, labels=labels, cex=0.6, pos=pos, data=data)
text(x=18, y=31, label="p<0.001", col="blue")






## Table A.9: Mosque Density and Ennahda’s Vote Share, 2011 and 2014 (OLS)

one <- lm(nahda11~mosque_pc_11+Abpray+pop2011+pct_2029_2014+pct_agri_2011+unemp_rate_2011+pct_higher_edu_2014+IVD_HR_pc+IVD_corr_pc, data=data)

two <- lm(nahda11~mosque_pc_11+nightlights14+pop2011+pct_2029_2014+pct_agri_2011+unemp_rate_2011+pct_higher_edu_2014+IVD_HR_pc+IVD_corr_pc, data=data)

three <- lm(nahda14~mosque_pc_11+Abpray+pop2011+pct_2029_2014+pct_agri_2014+unemp_rate_2014+pct_higher_edu_2014+IVD_HR_pc+IVD_corr_pc, data=data)

four <- lm(nahda14~mosque_pc_11+nightlights14+pop2011+pct_2029_2014+pct_agri_2014+unemp_rate_2014+pct_higher_edu_2014+IVD_HR_pc+IVD_corr_pc, data=data)

stargazer(one, two, three, four, covariate.labels=c("Mosques per 10,000","Piety (Survey)","Piety (Satellite)","Population","% age 20-29","% Agriculture","Unemployment","% Agriculture 2014","Unemployment 2014","% Higher Edu","IVD Human Rights", "IVD Corruption"))



## Figure 9: Mosque Density and Ennahda Vote Share in 2011, 2014, and 2019

## Figure 9A: Coefficient Plot, 2011
plot_model(one, title="Ennahda 2011 Vote Share", terms=c("mosque_pc_11","Abpray","pop2011","pct_2029_2014","pct_agri_2011","unemp_rate_2011","pct_higher_edu_2014","IVD_HR_pc","IVD_corr_pc"), axis.labels=rev(c("Mosques per 10,000","Piety","Population","% 20-29","% Agriculture","Unemployment","% Higher Edu","IVD Human Rights", "IVD Corruption")), show.values=T, value.offset=.4, type = "std", dot.size=1, value.size=3.4) + ylim(-.74, .6) + set_theme(geom.outline.color = "antiquewhite4", base = theme_bw()) + geom_hline(yintercept=0, color="black", size=.2) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), panel.border = element_blank()) + theme(axis.line = element_line(color = 'black'))

## Figure 9B: Effect size, 2011
plot(effect("mosque_pc_11", one), main="Effect of Mosque Density on Nahda Vote Share, 2011", ylab="Ennahda Vote Share", rug=F, xlab="Mosques per 10,000")

## Figure 9C: Coefficient Plot, 2014
plot_model(three, title="Ennahda 2014 Vote Share", terms=c("mosque_pc_11","Abpray","pop2011","pct_2029_2014","pct_agri_2014","unemp_rate_2014","pct_higher_edu_2014","IVD_HR_pc","IVD_corr_pc"), axis.labels=rev(c("Mosques per 10,000","Piety","Population","% 20-29","% Agriculture","Unemployment","% Higher Edu","IVD Human Rights", "IVD Corruption")), show.values=T, value.offset=.4, type = "std", dot.size=1, value.size=3.4) + ylim(-.5, .6) + set_theme(geom.outline.color = "antiquewhite4", base = theme_bw()) + geom_hline(yintercept=0, color="black", size=.2) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), panel.border = element_blank()) + theme(axis.line = element_line(color = 'black'))

## Figure 9D: Effect size, 2014
plot(effect("mosque_pc_11", three), main="Effect of Mosque Density on Nahda Vote Share, 2014", ylab="Ennahda Vote Share", rug=F, xlab="Mosques per 10,000")

## Figure 9E: Coefficient Plot, 2019
one <- lm(nahda19~mosque_pc_11+Abpray+pop2014+pct_2029_2014+pct_agri_2014+unemp_rate_2014+poverty+pct_higher_edu_2014+IVD_HR_pc+IVD_corr_pc+offerIsl_w+camp_mosque_w+imam_w, data=data)

plot_model(one, title="Ennahda 2019 Vote Share", terms=c("mosque_pc_11","Abpray","pop2014","pct_2029_2014","pct_agri_2014","unemp_rate_2014","poverty","pct_higher_edu_2014","IVD_HR_pc","IVD_corr_pc","offerIsl_w","camp_mosque_w","imam_w"), axis.labels=rev(c("Mosques per 10,000","Piety","Population","% 20-29","% Agriculture","Unemployment","Poverty","% Higher Edu","IVD Human Rights", "IVD Corruption","Vote-Buying","Campaigning", "Political Imam")), show.values=T, value.offset=.4, type = "std", dot.size=1, value.size=3.4) + ylim(-.5, .5) + set_theme(geom.outline.color = "antiquewhite4", base = theme_bw()) + geom_hline(yintercept=0, color="black", size=.2) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), panel.border = element_blank()) + theme(axis.line = element_line(color = 'black'))

## Figure 9F: Effect size, 2019
plot(effect("mosque_pc_11", one), main="Effect of Mosque Density on Nahda Vote Share, 2019", ylab="Ennahda Vote Share", rug=F, xlab="Mosques per 10,000 (2011)")




## Table A.10: Mosque Density and Islamist Vote Share in 2019 (OLS)

two <- lm(nahda19~mosque_pc_11+nightlights19+pop2014+pct_2029_2014+pct_agri_2014+unemp_rate_2014+poverty+pct_higher_edu_2014+IVD_HR_pc+IVD_corr_pc+offerIsl_w+camp_mosque_w+imam_w, data=data)

three <- lm(karama~mosque_pc_11+Abpray+pop2014+pct_2029_2014+pct_agri_2014+unemp_rate_2014+poverty+pct_higher_edu_2014+IVD_HR_pc+IVD_corr_pc+offerIsl_w+camp_mosque_w+imam_w, data=data)

four <- lm(karama~mosque_pc_11+nightlights19+pop2014+pct_2029_2014+pct_agri_2014+unemp_rate_2014+poverty+pct_higher_edu_2014+IVD_HR_pc+IVD_corr_pc+offerIsl_w+camp_mosque_w+imam_w, data=data)

five <- lm(islamist19~mosque_pc_11+Abpray+pop2014+pct_2029_2014+pct_agri_2014+unemp_rate_2014+poverty+pct_higher_edu_2014+IVD_HR_pc+IVD_corr_pc+offerIsl_w+camp_mosque_w+imam_w, data=data)

six <- lm(islamist19~mosque_pc_11+nightlights19+pop2014+pct_2029_2014+pct_agri_2014+unemp_rate_2014+poverty+pct_higher_edu_2014+IVD_HR_pc+IVD_corr_pc+offerIsl_w+camp_mosque_w+imam_w, data=data)

stargazer(one, two, three, four, five, six, covariate.labels=c("Mosques per 10,000","Piety (Survey)","Piety (Satellite)","Population","% age 20-29","% Agriculture","Unemployment","Poverty","% Higher Edu","IVD Human Rights", "IVD Corruption","Vote-Buying","Campaigning","Political Imam"))



## Figure A.8: Mosque Density and Islamist Vote Share in 2019

## Figure A.8A: Coefficient Plot, Karama Coalition
plot_model(three, title="Karama 2019 Vote Share", terms=c("mosque_pc_11","Abpray","pop2014","pct_2029_2014","pct_agri_2014","unemp_rate_2014","poverty","pct_higher_edu_2014","IVD_HR_pc","IVD_corr_pc","offerIsl_w","camp_mosque_w","imam_w"), axis.labels=rev(c("Mosques per 10,000","Piety","Population","% 20-29","% Agriculture","Unemployment","Poverty","% Higher Edu","IVD Human Rights", "IVD Corruption","Vote-Buying","Campaigning","Political Imam")), show.values=T, value.offset=.4, type = "std", dot.size=1, value.size=3.4) + ylim(-.7, .6) + set_theme(geom.outline.color = "antiquewhite4", base = theme_bw()) + geom_hline(yintercept=0, color="black", size=.2) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), panel.border = element_blank()) + theme(axis.line = element_line(color = 'black'))

## Figure A.8B: Effect size, Karama Coalition
plot(effect("mosque_pc_11", three), main="Effect of Mosque Density on Karama Vote Share", ylab="Karama Vote Share", rug=F, xlab="Mosques per 10,000 (2011)")


## Figure A.8C: Coefficient Plot, All Islamists
plot_model(five, title="Total Islamist 2019 Vote Share", terms=c("mosque_pc_11","Abpray","pop2014","pct_2029_2014","pct_agri_2014","unemp_rate_2014","poverty","pct_higher_edu_2014","IVD_HR_pc","IVD_corr_pc","offerIsl_w","camp_mosque_w","imam_w"), axis.labels=rev(c("Mosques per 10,000","Piety","Population","% 20-29","% Agriculture","Unemployment","Poverty","% Higher Edu","IVD Human Rights", "IVD Corruption","Vote-Buying","Campaigning","Political Imam")), show.values=T, value.offset=.4, type = "std", dot.size=1, value.size=3.4) + ylim(-.5, .5) + set_theme(geom.outline.color = "antiquewhite4", base = theme_bw()) + geom_hline(yintercept=0, color="black", size=.2) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), panel.border = element_blank()) + theme(axis.line = element_line(color = 'black'))

## Figure A.8D: Effect size, All Islamists
plot(effect("mosque_pc_11", five), main="Effect of Mosque Density on Total Islamist Vote Share", ylab="Islamist Vote Share", rug=F, xlab="Mosques per 10,000 (2011)")





## Table A.11: Using 2019 mosque density (OLS)

one <- lm(nahda19~mosque_pc_19+Abpray+pop2014+pct_2029_2014+pct_agri_2014+unemp_rate_2014+poverty+pct_higher_edu_2014+IVD_HR_pc+IVD_corr_pc+offerIsl_w+camp_mosque_w+imam_w, data=data)

two <- lm(nahda19~mosque_pc_19+nightlights19+pop2014+pct_2029_2014+pct_agri_2014+unemp_rate_2014+poverty+pct_higher_edu_2014+IVD_HR_pc+IVD_corr_pc+offerIsl_w+camp_mosque_w+imam_w, data=data)

three <- lm(karama~mosque_pc_19+Abpray+pop2014+pct_2029_2014+pct_agri_2014+unemp_rate_2014+poverty+pct_higher_edu_2014+IVD_HR_pc+IVD_corr_pc+offerIsl_w+camp_mosque_w+imam_w, data=data)

four <- lm(karama~mosque_pc_19+nightlights19+pop2014+pct_2029_2014+pct_agri_2014+unemp_rate_2014+poverty+pct_higher_edu_2014+IVD_HR_pc+IVD_corr_pc+offerIsl_w+camp_mosque_w+imam_w, data=data)

five <- lm(islamist19~mosque_pc_19+Abpray+pop2014+pct_2029_2014+pct_agri_2014+unemp_rate_2014+poverty+pct_higher_edu_2014+IVD_HR_pc+IVD_corr_pc+offerIsl_w+camp_mosque_w+imam_w, data=data)

six <- lm(islamist19~mosque_pc_19+nightlights19+pop2014+pct_2029_2014+pct_agri_2014+unemp_rate_2014+poverty+pct_higher_edu_2014+IVD_HR_pc+IVD_corr_pc+offerIsl_w+camp_mosque_w+imam_w, data=data)


stargazer(one, two, three, four, five, six, covariate.labels=c("Mosques per 10,000","Piety (Survey)", "Piety (Satellite)","Population","% age 20-29","% Agriculture","Unemployment","Poverty","% Higher Edu","IVD Human Rights", "IVD Corruption","Vote-Buying","Campaigning","Political Imam"))



## Table A.12: Mosques matter most where they are used (OLS)

one <- lm(nahda11~mosque_pc_11*mosque_daily+Abpray+pop2011+pct_2029_2014+pct_agri_2011+unemp_rate_2011+pct_higher_edu_2014+IVD_HR_pc+IVD_corr_pc, data=data)

two <- lm(nahda14~mosque_pc_11*mosque_daily+Abpray+pop2011+pct_2029_2014+pct_agri_2014+unemp_rate_2014+pct_higher_edu_2014+IVD_HR_pc+IVD_corr_pc, data=data)

three <- lm(nahda19~mosque_pc_11*mosque_daily+Abpray+pop2014+pct_2029_2014+pct_agri_2014+unemp_rate_2014+poverty+pct_higher_edu_2014+IVD_HR_pc+IVD_corr_pc+offerIsl_w+camp_mosque_w+imam_w, data=data)

four <- lm(karama~mosque_pc_11*mosque_daily+Abpray+pop2014+pct_2029_2014+pct_agri_2014+unemp_rate_2014+poverty+pct_higher_edu_2014+IVD_HR_pc+IVD_corr_pc+offerIsl_w+camp_mosque_w+imam_w, data=data)

five <- lm(islamist19~mosque_pc_11*mosque_daily+Abpray+pop2014+pct_2029_2014+pct_agri_2014+unemp_rate_2014+poverty+pct_higher_edu_2014+IVD_HR_pc+IVD_corr_pc+offerIsl_w+camp_mosque_w+imam_w, data=data)

stargazer(one, two, three, four, five, covariate.labels=c("Mosques per 10,000 (2011)","Mosque attendance (Survey)","Piety (Survey)","Population","Population (2014)","% age 20-29","% Agriculture","Unemployment","agri 2014","unemp 2014","Poverty","% Higher Edu","IVD Human Rights", "IVD Corruption","Vote-Buying","Campaigning","Political Imam"))


## Figure 11: Interacting Mosque Density with Mosque Attendance (2011-2019)

## Figure 11A: 2011
plot_model(one, type = "pred", terms = c("mosque_pc_11", "mosque_daily [0.054, 0.208, 0.363]"), title="Mosque Density*Attendance and Ennahda 2011 Vote Share", axis.title=c("Mosques per 10,000 people (2011)", "% Ennahda (2011)"), legend.title=c("Mosque\nAttendance")) + set_theme(geom.outline.color = "antiquewhite4", base = theme_bw()) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), panel.border = element_blank()) + theme(axis.line = element_line(color = 'black'))

## Figure 11B: 2014
plot_model(two, type = "pred", terms = c("mosque_pc_11", "mosque_daily [0.054, 0.208, 0.363]"), title="Mosque Density*Attendance and Ennahda 2014 Vote Share", axis.title=c("Mosques per 10,000 people (2011)", "% Ennahda (2014)"), legend.title=c("Mosque\nAttendance")) + set_theme(geom.outline.color = "antiquewhite4", base = theme_bw()) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), panel.border = element_blank()) + theme(axis.line = element_line(color = 'black'))

## Figure 11C: 2019
plot_model(three, type = "pred", terms = c("mosque_pc_11", "mosque_daily [0.054, 0.208, 0.363]"), title="Mosque Density*Attendance and Ennahda 2019 Vote Share", axis.title=c("Mosques per 10,000 people (2011)", "% Ennahda (2019)"), legend.title=c("Mosque\nAttendance")) + set_theme(geom.outline.color = "antiquewhite4", base = theme_bw()) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), panel.border = element_blank()) + theme(axis.line = element_line(color = 'black'))




## Table A.13: Change in Ennahda’s Votes, 2011-2019

one <- lm(nahda_change1119~mosque_pc_11+change_pop, data=data)

two <- lm(nahda_change1119~mosque_pc_11+change_pop+Abpray+pop2014+pct_2029_2014+pct_agri_2014+unemp_rate_2014+poverty+pct_higher_edu_2014+IVD_HR_pc+IVD_corr_pc+offerIsl_w+camp_mosque_w+imam_w, data=data)

three <- lm(nahda_change1119~mosque_pc_11+change_pop+nightlights19+pop2014+pct_2029_2014+pct_agri_2014+unemp_rate_2014+poverty+pct_higher_edu_2014+IVD_HR_pc+IVD_corr_pc+offerIsl_w+camp_mosque_w+imam_w, data=data)

four <- lm(nahda_change1119~mosque_pc_19, data=data)

five <- lm(nahda_change1119~mosque_pc_19+Abpray+pop2014+pct_2029_2014+pct_agri_2014+unemp_rate_2014+poverty+pct_higher_edu_2014+IVD_HR_pc+IVD_corr_pc+offerIsl_w+camp_mosque_w+imam_w, data=data)

six <- lm(nahda_change1119~mosque_pc_19+nightlights19+pop2014+pct_2029_2014+pct_agri_2014+unemp_rate_2014+poverty+pct_higher_edu_2014+IVD_HR_pc+IVD_corr_pc+offerIsl_w+camp_mosque_w+imam_w, data=data)

stargazer(one, two, three, four, five, six, covariate.labels=c("Mosques per 10,000 (2011)","New Mosques (2011-2019)","Piety (Survey)", "Piety (Satellite)","Population","% age 20-29","% Agriculture","Unemployment","Poverty","% Higher Edu","IVD Human Rights", "IVD Corruption","Vote-Buying","Campaigning","Political Imam","Mosques per 10,000 (2019)"))



## Figure 12: Mosque Density and Ennahda Vote Retention (2011-2019)

## Figure 12A: Mosque Density
plot(effect("mosque_pc_11", three), main="Effect of Mosque Density on Nahda Vote Retention, 2011-2019", ylab="Change in Ennahda Votes (%)", rug=F, xlab="Mosques per 10,000 (2011)")

## Figure 12B: New Mosques
plot(effect("change_pop", three), main="Effect of New Mosques on Nahda Vote Retention, 2011-2019", ylab="Change in Ennahda Votes (%)", rug=F, xlab="Change in mosque density, 2011-2019")

## Figure 12C: Coefficient Plot
plot_model(three, title="Change in Ennahda Vote Share, 2011-2019", terms=c("mosque_pc_11","change_pop","nightlights19","pop2014","pct_2029_2014","pct_agri_2014","unemp_rate_2014","poverty","pct_higher_edu_2014","IVD_HR_pc","IVD_corr_pc","offerIsl_w","camp_mosque_w","imam_w"), axis.labels=rev(c("Mosques per 10,000","New Mosques","Piety","Population","% 20-29","% Agriculture","Unemployment","Poverty","% Higher Edu","IVD Human Rights", "IVD Corruption","Vote-Buying","Campaigning", "Political Imam")), show.values=T, value.offset=.4, type = "std", dot.size=1, value.size=3.4) + ylim(-.4, .6) + set_theme(geom.outline.color = "antiquewhite4", base = theme_bw()) + geom_hline(yintercept=0, color="black", size=.2) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), panel.border = element_blank()) + theme(axis.line = element_line(color = 'black'))




## Table A.14: Ennahda voter retention disaggregated by election

one <- lm(nahda_change1114~mosque_pc_11, data=data)

two <- lm(nahda_change1114~mosque_pc_11+Abpray+pop2014+pct_2029_2014+pct_agri_2014+unemp_rate_2014+pct_higher_edu_2014+IVD_HR_pc+IVD_corr_pc, data=data)

three <- lm(nahda_change1419~mosque_pc_11, data=data)

four <- lm(nahda_change1419~mosque_pc_11+Abpray+pop2014+pct_2029_2014+pct_agri_2014+unemp_rate_2014+pct_higher_edu_2014+IVD_HR_pc+IVD_corr_pc, data=data)

stargazer(one, two, three, four, covariate.labels=c("Mosques per 10,000 (2011)","Piety (Survey)","Population","% age 20-29","% Agriculture","Unemployment","% Higher Edu","IVD Human Rights", "IVD Corruption"))



## Table A.15: Mediation: Trust in Ennahda

one <- lm(trust_nahda_w~mosque_pc_11, data=data)

two <- lm(nahda19~mosque_pc_11+Abpray+pop2014+pct_2029_2014+pct_agri_2014+unemp_rate_2014+poverty+pct_higher_edu_2014+IVD_HR_pc+IVD_corr_pc+offerIsl_w+camp_mosque_w+imam_w, data=data)

three <- lm(nahda19~mosque_pc_11+Abpray+pop2014+pct_2029_2014+pct_agri_2014+unemp_rate_2014+poverty+pct_higher_edu_2014+IVD_HR_pc+IVD_corr_pc+offerIsl_w+camp_mosque_w+imam_w+trust_nahda_w, data=data)

four <- lm(trust_nahda_w~mosque_pc_19, data=data)

five <- lm(nahda19~mosque_pc_19+Abpray+pop2014+pct_2029_2014+pct_agri_2014+unemp_rate_2014+poverty+pct_higher_edu_2014+IVD_HR_pc+IVD_corr_pc+offerIsl_w+camp_mosque_w+imam_w, data=data)

six <- lm(nahda19~mosque_pc_19+Abpray+pop2014+pct_2029_2014+pct_agri_2014+unemp_rate_2014+poverty+pct_higher_edu_2014+IVD_HR_pc+IVD_corr_pc+offerIsl_w+camp_mosque_w+imam_w+trust_nahda_w, data=data)

stargazer(one, two, three, four, five, six, dep.var.labels=c("Trust Ennahda","Ennahda Vote Share","Trust Ennahda","Ennahda Vote Share"), covariate.labels=c("Mosques per 10,000 (2011)","Piety (Survey)","Population","% age 20-29","% Agriculture","Unemployment","Poverty","% Higher Edu","IVD Human Rights", "IVD Corruption","Vote-Buying","Campaigning","Political Imam","Trust Ennahda","Mosques per 10,000 (2019)"))



## Figure A.10: Mediation Analyses, using mosque density from 2011 (left) and 2019 (right)

## Figure A.10A: 2011
med1 <- mediate(two, three, treat="mosque_pc_11", mediator="trust_nahda_w", sims=1000, boot=T)
summary(med1)

## Figure A.10B: 2019
med3 <- mediate(five, six, treat="mosque_pc_19", mediator="trust_nahda_w", sims=1000, boot=T)
summary(med3)




## Figure 10: Mediation: Mosque Density, Trust in Ennahda, and Ennahda Vote Share

## Figure 10A: Bivariate Correlation
data$labels <- ifelse(data$trust_nahda_w>6 | data$mosque_pc_11>15, as.character(data$delegation), "")

data$pos <- ifelse(data$delegation=="Matmata", 3,
      ifelse(data$delegation=="Sidi Aïch" | data$delegation=="Smâr", 3, 
      ifelse(data$delegation=="Ben Guerdane" | data$delegation=="Bab Souika", 1, 4)))

plot(y=data$trust_nahda_w, x=data$mosque_pc_11, main="Trust in Ennahda (2019-2020) by Mosque Density", ylab="Trust in Ennahda (1-10)", xlab="Mosques per 10,000 people (2011)")
abline(lm(trust_nahda_w~mosque_pc_11, data=data), col="blue")
text(trust_nahda_w~mosque_pc_11, labels=labels, cex=0.6, pos=pos, data=data)
text(x=18, y=5, label="p=0.015", col="blue")

## Figure 10B: Mediation Analysis
plot(med1, main="Effect of Mosque Density on Ennahda Vote Share\nMediated by Trust in Ennahda",  xlab="Ennahda Vote Share", yaxt="n") 
text(x=c(med1$d.avg, med1$z.avg, med1$tau.coef), y=c(3,2,1), labels=paste(round(c(med1$d.avg, med1$z.avg, med1$tau.coef),3), c("*","","*"),sep=" "), pos=3)
axis(side=2, at=c(3,2,1), labels=c("Mediated\nEffect","Direct\nEffect","Total\nEffect"), las=2, cex.axis=0.9) 


## Figure A.11: Strategic construction of mosques, 2011 to 2019

data$labels <- ifelse(data$change_pop>4, as.character(data$delegation), "")

data$pos <- ifelse(data$delegation=="" | data$delegation=="Bargou" | data$delegation=="Amdoun" |  data$delegation=="Mornag", 2,
      ifelse(data$delegation=="El Aroussa" | data$delegation=="Korba" | data$delegation=="" | data$delegation=="" , 3, 
      ifelse(data$delegation=="Regueb" | data$delegation=="", 1, 4)))

plot(y=data$change_pop, x=data$nahda11, main="New Mosques (2011-2019) by Nahda 2011 Vote Share", ylab="Percent Change in Mosques, 2011-2019", xlab="Ennahda Vote Share 2011")
abline(lm(change_pop~nahda11, data=data), col="blue")
text(change_pop~nahda11, labels=labels, cex=0.6, pos=pos, data=data)
text(x=60, y=0, label="p=0.005", col="blue")



## Table A.16: Strategic construction of mosques, 2011 to 2019

one <- lm(change_pop~nahda11, data=data)

two <- lm(change_pop~nahda11+Abpray+pop2014+popchange+pct_2029_2014+pct_agri_2014+unemp_rate_2014+poverty+pct_higher_edu_2014+IVD_HR_pc+IVD_corr_pc, data=data)

three <- lm(change_pop~nahda11+nightlights19+pop2014+popchange+pct_2029_2014+pct_agri_2014+unemp_rate_2014+poverty+pct_higher_edu_2014+IVD_HR_pc+IVD_corr_pc, data=data)

stargazer(one, two, three, covariate.labels=c("Ennahda Vote Share (2011)","Piety (Survey)", "Piety (Satellite)","Population 2014","Population Growth, 2004-14","% age 20-29","% Agriculture","Unemployment","Poverty","% Higher Edu","IVD Human Rights", "IVD Corruption"))



@







<<>>=

############################
## Test 3: Arab Barometer ##
############################


## Table A.17: Friday Prayers and Trust in Islamists (OLS)

one <- lm(islamist~friday+pray+sharia+nonmuslim+menedu+antiUS+antiIsrael+gov_ineq+elimcorr+cand_integrity+cand_piety+suit+age+female+married+edu+income+unemp+country, data=AB[AB$wave=="W2" & AB$muslim==1,])
two <- lm(islamist~friday+pray+sharia+nonmuslim+menedu+antiUS+antiIsrael+gov_ineq+elimcorr+cand_integrity+cand_piety+suit+age+female+married+edu+income+unemp+country, data=AB[AB$wave=="W3" & AB$muslim==1,])
three <- lm(islamist~friday+pray+sharia+nonmuslim+menedu+antiUS+antiIsrael+gov_ineq+elimcorr+relcorr+suit+age+female+married+edu+income+unemp+country, data=AB[AB$wave=="W5" & AB$muslim==1,])

stargazer(one, two, three, covariate.labels=c("Friday Prayers","Frequency of Prayer","Support for Sharia","$<$ rights for Non-Muslims","Men $>$ Women in Edu","Anti-US","Anti-Israel", "Inequality","Corruption","Want Integrity","Want Piety","Rel not Corrupt","Democracy", "Age","Female","Married","Education","Income","Unemployed"), single.row=T)


## Figure 13: Friday Prayers at the Mosque and Trust in Islamists (Arab Barometer)

plot_model(one, title="Trust in Islamists (Arab Barometer 2011)", terms=c("friday","pray","sharia","nonmuslim","menedu","antiUS","antiIsrael","gov_ineq","elimcorr","cand_integrity","cand_piety","suit","age","female","married","edu","income","unemp"), axis.labels=rev(c("Friday Prayers (Mosque)","Frequency of Prayer","Support for Sharia","< rights for Non-Muslims","Men > Women in Edu", "Anti-US","Anti-Israel","Inequality","Corruption","Want Integrity","Want Piety","Support for Democracy", "Age","Female","Married","Education","Income","Unemployed")), show.values=T, value.offset=.4, type = "std", dot.size=1, value.size=3.4) + ylim(-.2, .26) + set_theme(geom.outline.color = "antiquewhite4", base = theme_bw()) + geom_hline(yintercept=0, color="black", size=.2) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), panel.border = element_blank()) + theme(axis.line = element_line(color = 'black'))

plot_model(two, title="Trust in Islamists (Arab Barometer 2013)", terms=c("friday","pray","sharia","nonmuslim","menedu","antiUS","antiIsrael","gov_ineq","elimcorr","cand_integrity","cand_piety","suit","age","female","married","edu","income","unemp"), axis.labels=rev(c("Friday Prayers (Mosque)","Frequency of Prayer","Support for Sharia","< rights for Non-Muslims","Men > Women in Edu", "Anti-US","Anti-Israel","Inequality","Corruption","Want Integrity","Want Piety","Support for Democracy", "Age","Female","Married","Education","Income","Unemployed")), show.values=T, value.offset=.4, type = "std", dot.size=1, value.size=3.4) + ylim(-.22, .18) + set_theme(geom.outline.color = "antiquewhite4", base = theme_bw()) + geom_hline(yintercept=0, color="black", size=.2) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), panel.border = element_blank()) + theme(axis.line = element_line(color = 'black'))

plot_model(three, title="Trust in Islamists (Arab Barometer 2019)", terms=c("friday","pray","sharia","nonmuslim","menedu","antiUS","antiIsrael","gov_ineq","elimcorr","relcorr","suit","age","female","married","edu","income","unemp"), axis.labels=rev(c("Friday Prayers (Mosque)","Frequency of Prayer","Support for Sharia","< rights for Non-Muslims","Men > Women in Edu", "Anti-US","Anti-Israel","Inequality","Corruption","Rel not Corrupt","Support for Democracy", "Age","Female","Married","Education","Income","Unemployed")), show.values=T, value.offset=.4, type = "std", dot.size=1, value.size=3.4) + ylim(-.14, .17) + set_theme(geom.outline.color = "antiquewhite4", base = theme_bw()) + geom_hline(yintercept=0, color="black", size=.2) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), panel.border = element_blank()) + theme(axis.line = element_line(color = 'black'))


plot(effect("friday", one), main="Effect of Friday Prayers, 2011", ylab="Trust in Islamists (1-4)", rug=F, xlab="Attendance at Friday Prayers (5=Always)")
plot(effect("friday", two), main="Effect of Friday Prayers, 2013", ylab="Trust in Islamists (1-4)", rug=F, xlab="Attendance at Friday Prayers (5=Always)")
plot(effect("friday", three), main="Effect of Friday Prayers, 2019", ylab="Trust in Islamists (1-4)", rug=F, xlab="Attendance at Friday Prayers (5=Always)")




## Table A.18: Friday Prayers and Trust in Islamists, by Gender (OLS)

one <- lm(islamist~friday*female+pray+sharia+nonmuslim+menedu+antiUS+antiIsrael+gov_ineq+elimcorr+cand_integrity+cand_piety+suit+age+female+married+edu+income+unemp+country, data=AB[AB$wave=="W2" & AB$muslim==1,])
two <- lm(islamist~friday*female+pray+sharia+nonmuslim+menedu+antiUS+antiIsrael+gov_ineq+elimcorr+cand_integrity+cand_piety+suit+age+female+married+edu+income+unemp+country, data=AB[AB$wave=="W3" & AB$muslim==1,])
three <- lm(islamist~friday*female+pray+sharia+nonmuslim+menedu+antiUS+antiIsrael+gov_ineq+elimcorr+relcorr+suit+age+female+married+edu+income+unemp+country, data=AB[AB$wave=="W5" & AB$muslim==1,])

library(stargazer)
stargazer(one, two, three, covariate.labels=c("Friday Prayers","Female","Frequency of Prayer","Support for Sharia","$<$ rights for Non-Muslims","Men $>$ Women in Edu","Anti-US","Anti-Israel", "Inequality","Corruption","Want Integrity","Want Piety","Rel not Corrupt","Democracy", "Age","Married","Education","Income","Unemployed"), single.row=T)



@




\end{document}
